<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\model\InviteModel;
use think\facade\Session;
use think\facade\View;
use think\facade\Db;

class Invite extends Common
{
    public function getList(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $keyword = input('keyword');
        $map = [];
        if(!empty($keyword)){
            $map[] = ['ip_list|name','like','%'.$keyword.'%'];
        }
        $list = InviteModel::pageList($map);
	        foreach($list as $key=>$val){
	        $val->titlemes = InviteModel::where('id',$val->pcode)->value('ip_list');
	        $val->pidnewsid = '/news/show-'.$val->pid.'.html#'.$val->id.'';
	}

        return json(['code'=>0,'msg'=>'success','data'=>$list]);
    }

    public function del(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $id = $this->request->param('ids');
        $res = InviteModel::destroy($id,true);
        if($res){
            return json(['code'=>0,'msg'=>'删除成功']);
        }else{
            return json(['code'=>1,'msg'=>'删除失败']);
        }
    }

    public function add(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $data = $this->request->param('data');
		//确认传入参数种是否存在ID
        if(!empty($data['id'])){
            InviteModel::update($data);
            return json(['code'=>0,'msg'=>'编辑成功']);
        }else{
            unset($data['id']);
            InviteModel::create($data);
            return json(['code'=>0,'msg'=>'添加成功']);
        }
    }
    
    // 管理留言
    public function editShu() {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $admin_id = Session::get('admin_id');
        $info = Session::get('adminInfo');
        $data = $this->request->param('data');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'非法请求']);
        }

        $userList = Db::name('invite')->order('create_time DESC')->select();
        if ($this->request->isPost()) {
            $id         = input('post.id');
            $value      = input('post.value');
            $action     = input('post.action');
            $status     = input('post.status');
            $category   = input('post.category');
            if($value == 'true'){
                $value = '1';
            }elseif($value == 'false'){
                $value = '0';
            }
            // 更新留言信息
            if($category == 'status'){
                Db::name('invite')->where('id', $id)->update([$status => $value]);
            }else{
                Db::name('invite')->update([$category => $value,'id'=>$id]);
            }
            return json(['code'=>0,'msg'=>'操作成功']);
        }
        $this->getSide();
        return $this->fetch();
    }
    
}